/****************************************************************/
/*  I M P E T U S     C O P Y R I G H T      S T A T E M E N T  */
/****************************************************************/
/*                                                              */
/*  NOTICE: THIS SOFTWARE CONTAINS TRADE SECRETS THAT BELONG TO */
/*          IMPETUS INFOTECH, AND IS LICENSED BY AN AGREEMENT.  */
/*          ANY UNAUTHORIZED ACCESS, USE, DUPLICATION OR        */
/*          DISCLOSURE IS UNLAWFUL.                             */
/*                                                              */
/*          COPYRIGHT (C) 2008 IMPETUS INFOTECH INDIA PVT LTD   */
/*          ALL RIGHTS RESERVED.                                */
/****************************************************************/
package com.iLabs.custom.component;

import java.io.IOException;
import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 * This is a custom JSF component that renders the error message on the page
 * 
 */
public class ErrorHandler extends UIOutput {

	/* (non-Javadoc)
	 * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
	 */
	public void encodeBegin(FacesContext arg0) throws IOException {
		ResponseWriter writer= arg0.getResponseWriter();
		writer.startElement("DIV", this);
		writer.writeAttribute("class", getAttributes().get("styleClass"), "");
		Iterator messages=arg0.getMessages();
		while (messages.hasNext()) {
			FacesMessage currentMessage=(FacesMessage)messages.next();
			writer.write(currentMessage.getDetail());
			writer.write("<BR>");
			messages.remove();
		}
	}

	/* (non-Javadoc)
	 * @see javax.faces.component.UIComponentBase#encodeEnd(javax.faces.context.FacesContext)
	 */
	public void encodeEnd(FacesContext arg0) throws IOException {
		ResponseWriter writer= arg0.getResponseWriter();
		writer.endElement("DIV");
	}

	
}
